<template>
  <div>
    <el-dialog title="上传图纸" :visible.sync="dialogFormVisible" width="40%">
      <el-form :model="form" status-icon ref="form">
        <el-form-item label="户型" :label-width="formLabelWidth" prop="projectId">
          <el-select v-model="form.apartmentId" placeholder="请选择户型" style="width:83%">
            <el-option
              v-for="item in apartmentList"
              :key="item.apartmentId"
              :label="item.apartmentName"
              :value="item.apartmentId">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图纸格式" :label-width="formLabelWidth" prop="exchange">
          <el-select v-model="exchange.exchange" placeholder="请选择图纸格式" style="width:83%">
            <el-option
              v-for="item in paperTypeList"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="图纸上传" :label-width="formLabelWidth" prop="paperUrl">
          <el-upload
            class="upload-demo"
            action="http://jasobim.com:8085/api/files/uploadFiles"
            :on-success="returnList"
            name="file"
            :data="exchange"
            v-model="form.paperUrl"
            auto-upload
            list-type="picture"
            :file-list="fileList"
            multiple>
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="resetForm('form')">取 消</el-button>
        <el-button type="primary" @click="submit('form')">确 定</el-button>
      </div>
    </el-dialog>
    <!-- 新增弹出框 -->
    <el-button type="primary" style="margin:10px 30px;float:right; " @click="openAddPage" :disabled="addShow">上传图纸</el-button>
    <!-- 新增弹出框底层 -->
    <el-button type="danger" @click="deleteSelect" style="margin:10px 5px; float:right;">删除选中</el-button>
    <el-table
      :data="tableData"
      style="width: 100%"
      :row-class-name="tableRowClassName"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        width="55">
      </el-table-column>
      <el-table-column
        prop="measurePaperName"
        label="图纸名称">
      </el-table-column>
      <el-table-column
        prop="projectId"
        label="项目名称">
        <template slot-scope="scope">
          <template v-for="item in projects">
            {{scope.row.projectId=== item.projectId ? item.projectName : ''}}
          </template>
        </template>
      </el-table-column>
      <el-table-column
        prop="type"
        label="类型">
        <template slot-scope="scope">
          {{options[scope.row.type-1]}}
        </template>
      </el-table-column>
      <el-table-column
        prop="measureNum"
        label="测量点数">
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="上传时间"
        :formatter="formatDate">
      </el-table-column>
      <el-table-column
        prop="measurePaperStatus"
        label="图纸状态">
        <template slot-scope="scope">
          {{statusList[scope.row.measurePaperStatus-1]}}
        </template>
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button type="text" @click="setPoint(scope.row)">标注</el-button>
          <el-button type="text" style="color:red;" @click="deleteSelects(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="block">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page.currentPage"
        :page-sizes="[10, 20, 30, 40]"
        :page-size="page.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="page.total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
  import { mapGetters } from 'vuex'
  import { getAllApartment,getProjectCheckTypeLists, addMeasurePaper, deleteMeasurePaper, getMeasurePaperList } from '@/api/measure'
  import { formatDate1 } from '@/api/project'
  import Drawer from '../components/Drawer'

  var pageParams = { page: { pageSize: 10, pageNo: 1 } }
  export default {
    components: {
      Drawer
    },
    computed: {
      ...mapGetters([
        'currentProject',
        'projects',
        'userInfo',
        'token'
      ])
    },
    watch: {
      currentProject: function(n, o) {
        this.initDatas()
      }
    },
    
    data() {
      return {
        myHeaders: {
            Authorization:'Bearer ',
        },
        exchange:{exchange:0},
        paperTypeList:[{"label":"图片","value":0},{"label":"PDF","value":1}],
        options: ['设备安装', '土建'],
        statusList: ['未标注', '已标注'],
        page: {
          currentPage: 1,
          pageSize: 10,
          total: 0
        },
        addShow:false,
        tableData: [],
        checkTypeTable: [],
        /*新增弹出框操作*/
        dialogTableVisible: false,
        dialogFormVisible: false,
        dialogPointVisible: false,
        form: {
          measurePaperName: '',
          apartmentId:'',
          projectId: '',
          paperUrl: ''
        },
        apartmentList:[],
        fileList: [],
        formLabelWidth: '120px',
        //////////////////////////
        /*选中删除*/
        multipleSelection: []
      }
    },
    methods: {
      event(params) {
        console.log('event-params', params)
      },
      setPoint(row) {
        this.dialogPointVisible = true
        console.log('paperUrl',row.paperUrl);
        getProjectCheckTypeLists({ 'checkType': 1 }).then(res => {
          sessionStorage.setItem('items', JSON.stringify(res.data))
          this.$router.push({
            path: '/project/drawerPage',
            query: { data: res.data, projectId: this.currentProject.projectId, measurePaperId: row.measurePaperId,paperUrl:row.paperUrl }
          })
          this.checkTypeTable = res.data
        })
      },
      getProjectCheckTypeList() {

      },
      formatDate(row, column) {
        let date = new Date(row.createTime)
        return formatDate1(date)
      },
      returnList(response, file, fileList) {
        this.form.paperUrl = response.data[0]
        console.log('file', file)
        console.log('fileList', fileList)
      },
      //新增页面
      openAddPage() {
        this.form = {};
        this.getAllApartmentList();
        this.dialogFormVisible = true
      },
      resetForm(form) {
        /*debugger
        this.$refs[form].resetFields();*/
        this.dialogFormVisible = false
      },
      updateMachinery(row) {
        //编辑页面
        this.form = Object.assign({}, row)
        this.dialogFormVisible = true
      },
       //获取所有户型
      getAllApartmentList(){
        getAllApartment({'projectId':this.currentProject.projectId}).then(res=>{
          this.apartmentList = res.data;
        });
      },
      //提交
      submit(form) {
        debugger
        if(this.form.apartmentId!=undefined){
          for(var i=0;i<this.apartmentList.length;i++){
            if(this.apartmentList[i].apartmentId==this.form.apartmentId){
              this.form.measurePaperName = this.apartmentList[i].apartmentName;
            }
          }
        }
        this.form.projectId = this.currentProject.projectId
        addMeasurePaper(this.form).then(res => {
          this.initDatas()
          this.dialogFormVisible = false
          this.$message.success(res.message)
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      //删除选中
      deleteSelect(row) {
        deleteMeasurePaper(this.multipleSelection).then(res => {
          this.$message.success(res.message)
          this.initDatas()
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      //删除按钮
      deleteSelects(row) {
        deleteMeasurePaper([row]).then(res => {
          this.$message.success(res.message)
          this.initDatas()
        }).catch(res => {
          this.$message.error(res.message)
        })
      },
      handleSelectionChange(val) {
        this.multipleSelection = val
      },
      initDatas() {
        this.tableData = undefined
        this.loading = this.$route.query.loading
        getMeasurePaperList({
          'pageVo': {
            'pageSize': pageParams.page.pageSize,
            'pageNo': pageParams.page.pageNo
          },
          'projectId': this.currentProject.projectId
        }).then(res => {
          this.tableData = res.data.data
          this.page.total = res.data.page.total
          this.getAllApartmentList();
        }).catch(res => {

        })
      },
      tableRowClassName({ row, rowIndex }) {
        if (rowIndex === 1) {
          return 'warning-row'
        } else if (rowIndex === 3) {
          return 'success-row'
        }
        return ''
      },
      handleSizeChange(val) {
        pageParams.page.pageSize = val
        this.initDatas()
        console.log(`每页 ${val} 条`)
      },
      handleCurrentChange(val) {
        pageParams.page.pageNo = val
        this.initDatas()
        console.log(`当前页: ${val}`)
      },
      setToken(){
        this.myHeaders.Authorization=this.myHeaders.Authorization+this.token;
      }
    },
    created() {
      console.log(this.token)
      this.initDatas()
      this.setToken();
    }
  }
</script>
